﻿using System;
using System.Windows.Forms;
using System.Configuration;
using System.Net;

namespace DATA_SERVER
{
    public partial class ConfigForm : Form
    {
        private static string CenterIP;
        private static int CenterPort;
        public static bool ReInit;
        public static bool Resync;

        public ConfigForm()
        {
            InitializeComponent();
        }

        //窗口加载
        private void ConfigForm_Load(object sender, EventArgs e)
        {
            try
            {
                string[] dbarr = Global.DBConnectionString.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                tb_dbip.Text = dbarr[0].Substring(7).Trim();
                tb_dbname.Text = dbarr[1].Substring(9).Trim();
                tb_dbuser.Text = dbarr[2].Substring(4).Trim();
                tb_dbpwd.Text = dbarr[3].Substring(4).Trim();
                cb_center.Checked = Global.HaveCenter;
                tb_centerip.Text = CenterIP;
                tb_centerport.Text = CenterPort.ToString();
                tb_myport.Text = Global.ServerPort.ToString();
                cb_autosync.Checked = Global.AutoSync;
                cb_tag.Checked = Global.UseTag;
            }
            catch { }
        }

        //点击保存按钮
        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                Global.DBConnectionString = string.Format("server={0};database={1};uid={2};pwd={3};Connect Timeout=5", tb_dbip.Text, tb_dbname.Text, tb_dbuser.Text, tb_dbpwd.Text);
                Global.HaveCenter = cb_center.Checked;
                CenterIP = tb_centerip.Text;
                CenterPort = tb_centerport.Text == "" ? 0 : int.Parse(tb_centerport.Text);
                Global.CenterEP = CenterIP == "" ? null : new IPEndPoint(IPAddress.Parse(CenterIP), CenterPort);
                Global.ServerPort = tb_myport.Text == "" ? 0 : int.Parse(tb_myport.Text);
                Global.AutoSync = cb_autosync.Checked;
                Global.UseTag = cb_tag.Checked;
                SaveConfig();
            }
            catch { }
            this.Close();
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //读取配置文件
        public static bool ReadConfig()
        {
            try
            {
                Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                Global.MachCode = config.AppSettings.Settings["门禁机类型"].Value;
                Global.DBConnectionString = config.AppSettings.Settings["数据库连接字符串"].Value;
                Global.HaveCenter = (config.AppSettings.Settings["人员定位"].Value != "0");
                CenterIP = config.AppSettings.Settings["人员定位IP"].Value;
                CenterPort = config.AppSettings.Settings["人员定位端口号"].Value == "" ? 0 : int.Parse(config.AppSettings.Settings["人员定位端口号"].Value);
                Global.CenterEP = CenterIP == "" ? null : new IPEndPoint(IPAddress.Parse(CenterIP), CenterPort);
                Global.ServerPort = config.AppSettings.Settings["本机端口号"].Value == "" ? 0 : int.Parse(config.AppSettings.Settings["本机端口号"].Value);
                Global.AutoSync = (config.AppSettings.Settings["自动同步用户"].Value != "0");
                Global.UseTag = (config.AppSettings.Settings["使用标识卡作为门禁卡"].Value != "0");
                ReInit = false;
                Resync = false;
                return true;
            }
            catch
            {
                return false;
            }
        }

        //保存配置文件
        public static void SaveConfig()
        {
            try
            {
                Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings["数据库连接字符串"].Value != Global.DBConnectionString)
                {
                    ReInit = true;
                }

                if (config.AppSettings.Settings["使用标识卡作为门禁卡"].Value != (Global.UseTag ? "1" : "0"))
                {
                    Resync = true;
                }

                config.AppSettings.Settings["门禁机类型"].Value = Global.MachCode;
                config.AppSettings.Settings["数据库连接字符串"].Value = Global.DBConnectionString;
                config.AppSettings.Settings["人员定位"].Value = Global.HaveCenter ? "1" : "0";
                config.AppSettings.Settings["人员定位IP"].Value = CenterIP;
                config.AppSettings.Settings["人员定位端口号"].Value = CenterPort > 0 ? CenterPort.ToString() : "";
                config.AppSettings.Settings["本机端口号"].Value = Global.ServerPort > 0 ? Global.ServerPort.ToString() : "";
                config.AppSettings.Settings["自动同步用户"].Value = Global.AutoSync ? "1" : "0";
                config.AppSettings.Settings["使用标识卡作为门禁卡"].Value = Global.UseTag ? "1" : "0";
                config.Save(ConfigurationSaveMode.Modified);
                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            }
            catch { }
        }
    }
}
